<?php

require_once 'Zend/Db/Table/Abstract.php';
class Model_LocationMapper extends Zend_Db_Table_Abstract {

	protected $_name = 'location';
	protected $_primary = 'LocID';
	//protected $_sequence = true;
	protected $_dependentTables = array('dish_location');
	
	public function getAllLocations()
	{
		$rowset=$this->fetchAll();
		$rowCount = count($rowset);
		$result=Array();
		$i=0;
		if ($rowCount>0)
		{
			foreach ($rowset as $row) {
				$i=$i+1;
				$name = $row->Name;
				$id = $row->LocID;
				
				$disId=$row->DisID;
				$disMapper=new Model_DistrictMapper();
				$district=$disMapper->getDistrict($disId);
				
				$address=$row->Address;
				$phone=$row->Phone;
				$description=$row->Description;
				$image=$row->Image;
				$star=$row->Star_Rating;
				$loc=new Model_Location();
				$loc->setId($id)->setDistrict($district)->setName($name)->setAddress($address)->setPhone($phone)->setDescription($description)->setImage($image)->setStar($star);
				$result[$i]=$loc;
			}
		}
		return $result;
	}
	public function getTop1Locations(){
		return $this->getLocation(1);
	}
	
	public function getPopularLocations()
	{
		$rowset=$this->fetchAll();
		$rowCount = count($rowset);
		$result=Array();
		$i=0;
		if ($rowCount>0)
		{
			foreach ($rowset as $row) {
				$i=$i+1;
				$name = $row->Name;
				$id = $row->LocID;
	
				$disId=$row->DisID;
				$disMapper=new Model_DistrictMapper();
				$district=$disMapper->getDistrict($disId);
	
				$address=$row->Address;
				$phone=$row->Phone;
				$description=$row->Description;
				$image=$row->Image;
				$star=$row->Star_Rating;
				$loc=new Model_Location();
				$loc->setId($id)->setDistrict($district)->setName($name)->setAddress($address)->setPhone($phone)->setDescription($description)->setImage($image)->setStar($star);
				$result[$i]=$loc;
				if ($i>3) return $result;
			}
		}
		return $result;
	}
	
	
	public function getLocation($locId)
	{
		$row=$this->fetchRow($this->select()->where('LocID=?',$locId));
		$result=new Model_Location();
		if ($row)
		{
			$name = $row->Name;
				$id = $row->LocID;
				
				$disId=$row->DisID;
				$disMapper=new Model_DistrictMapper();
				$district=$disMapper->getDistrict($disId);
				
				$address=$row->Address;
				$phone=$row->Phone;
				$description=$row->Description;
				$image=$row->Image;
				$star=$row->Star_Rating;
				$loc=new Model_Location();
				$loc->setId($id)->setDistrict($district)->setName($name)->setAddress($address)->setPhone($phone)->setDescription($description)->setImage($image)->setStar($star);
				$result=$loc;
		}
		return $result;
	}
	
	
	public function getLocationsByDistrict($disId){
		$rowset=$this->fetchAll($this->select()->where('DisID=?',$disId))->toArray();
		$rowCount = count($rowset);
		$result=Array();
		$i=0;
		if ($rowCount>0)
		{
			foreach ($rowset as $row) {
				$i=$i+1;
				$name = $row['Name'];
				$id = $row['LocID'];
		
				$disId=$row['DisID'];
				$disMapper=new Model_DistrictMapper();
				$district=$disMapper->getDistrict($disId);
		
				$address=$row['Address'];
				$phone=$row['Phone'];
				$description=$row['Description'];
				$image=$row['Image'];
				$star=$row->Star_Rating;
				$loc=new Model_Location();
				$loc->setId($id)->setDistrict($district)->setName($name)->setAddress($address)->setPhone($phone)->setDescription($description)->setImage($image)->setStar($star);
				
				$result[$i]=$loc;
			}
		}
		return $result;
	}
	public function createLocation(Model_Location $loc)
	{
		try {
			$data=array(
					'DisID'     	=> $loc->getDistrict()->getId(),
					'Name' 			=> $loc->getName(),
					'Image'      	=> $loc->getImage(),
					'Description'	=> $loc->getDescription(),
					'Address'	=> $loc->getAddress(),
					'Phone'	=> $loc->getPhone(),
					'Star_Rating'	=> $loc->getStar(),
			);
			$this->insert($data);
			return "da tao";
		} catch (Exception $e) {
			return "error";
		}
	
	}
	
	public function getTotalLocationsByDistrict($disId){
		$rowset=$this->fetchAll($this->select()->where('DisID=?',$disId))->toArray();
		$rowCount = count($rowset);
		return $rowCount;
	}
	
	public function getCategoriesOfLocation(Model_Location $loc){
		$row=$this->fetchRow($this->select()->where('LocID=?',$loc->getId()));
		$dishes=$row->findManyToManyRowset('Model_DishMapper', 'Model_DishLocationMapper');
		$catM= new Model_CategoryMapper();
		$result=Array();
		$i=0;
		foreach ( $dishes as $d ) {
			$cat =$catM->getCategory($d['CatID']);
			$exist=$this->isExist($result, $cat);
			if ($exist==false){
				$result[$i]=$cat;
				$i++;
			}
		}
		return $result;
	}
	/*public function getDeliveryOfLocation(Model_Location $loc){
		$row=$this->fetchRow($this->select()->where('LocID=?',$loc->getId()));
		$delvs=$row->findManyToManyRowset('Model_DeliveryMapper');
		$delvM= new Model_DeliveryMapper();
		$result=Array();
		$i=0;
		foreach ( $delvs as $d ) 
		{
			
			$del =$delvM->getDelivery($d['DelID']);
			$exist=$this->isExistDel($result, $del);
			if ($exist==false){
				$result[$i]=$del;
				$i++;
			}
		}
		return $result;
	}*/
	public function isExist(Array $s, Model_Category $c){
		foreach ( $s as $cc ) {
			if ($c->getId()==$cc->getId()){
				return true;
			}
		}
		return false;
	} 
	public function isExistDel(Array $s, Model_Delivery $c){
		foreach ( $s as $cc ) {
			if ($c->getId()==$cc->getId()){
				return true;
			}
		}
		return false;
	}
	public function removeLocation($LocId)
	{
		try {
			$registry 	= Zend_Registry::getInstance();
			$DB = $registry['my_db'];
			$DB->delete('Location', 'LocID = '.$LocId);
			return true;
		}
		catch (Exception $e){
			return  false;
		}
	
	
	}
	
	public function updateLocation(Model_Location $loc)
	{
	
		try {
			$data=array(
					'DisID'     	=> $loc->getDistrict()->getId(),
					'Name' 			=> $loc->getName(),
					'Image'      	=> $loc->getImage(),
					'Description'	=> $loc->getDescription(),
					'Address'	=> $loc->getAddress(),
					'Phone'	=> $loc->getPhone(),
					'Star_Rating'	=> $loc->getStar(),
			);
			$where=Array('LocID = ?' => $loc->getId()
			);
			$this->update($data, $where);
			return "";
		} catch (Exception $e) {
			return "error";
		}
	
	
	}
	
	public function findLocations($query)
	{
		$select = $this->select();
		$select->where('Name LIKE ?', '%'.$query.'%');
		$rowset = $this->fetchAll($select);
	
		$rowcount= count($rowset);
		$result=Array();
		$i=0;
		if($rowcount>0)
		{
			foreach ($rowset as $row)
			{
				$i=$i+1;
				$name = $row->Name;
				$id = $row->LocID;
				$disId=$row->DisID;
				$image=$row->Image;
				$address=$row->Address;
				$phone=$row->Phone;
				$star=$row->Star_Rating;
				$disMapper=new Model_DistrictMapper();
				$district=$disMapper->getDistrict($disId);
	
				$description=$row->Description;
				$loc=new Model_Location();
				$loc->setId($id)->setName($name)->setDistrict($district)->setDescription($description)->setImage($image)->setAddress($address)->setPhone($phone)->setStar($star);
				$result[$i]=$loc;
			}
		}
		return $result;
	}
	
}